/*
 * Created by JFormDesigner on Mon Mar 28 13:42:14 CST 2022
 */

package com.gxu.sjjg;

import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.*;
import javax.swing.border.*;

/**
 * @author unknown
 */
public class Modify extends JFrame {
    public Modify() {
        initComponents();
    }

    private void ok(ActionEvent e) {
        boolean error=false;
        String regex="[1-9]\\d{1,2}";
        String match1="[\\u4e00-\\u9fa5]{1,6}";//汉字的unicode编码范围为\u4e00-\u9fa5,
        if(!textField1.getText().matches(match1)&&textField1.getText().length()>0||textField3.getText().length()>0&&(!textField3.getText().matches(regex)||!(Integer.parseInt(textField3.getText())<101)) )
            error=true;
        else if(textField4.getText().length()>0&&(!textField4.getText().matches(regex)||!(Integer.parseInt(textField4.getText())<101)) )
            error=true;
        else if(textField5.getText().length()>0&&(!textField5.getText().matches(regex)||!(Integer.parseInt(textField5.getText())<101)) )
            error=true;
        else if(textField6.getText().length()>0&&(!textField6.getText().matches(regex)||!(Integer.parseInt(textField6.getText())<101)) )
            error=true;
        if(error){
            JOptionPane.showMessageDialog(null, "数据不合法！", "提示", JOptionPane.WARNING_MESSAGE);
        }
        else {
            try {
                String[] imf=new String[100];
                int j=0;
                BufferedReader fbr = new BufferedReader(new FileReader("src/file/Grade.txt"));
                imf[j++]=fbr.readLine();//先把第一行读完
                String s;
                boolean exsist = false;
                while ((s = fbr.readLine()) != null) {
                    if (s.substring(10, 19).trim().equals(textField2.getText())) {//如果不是该学生，就添加到数组，是的话就不添加,而是添加输入值
                        //存入数组
                        imf[j]="";
                        int len;
                        if((len = 10 - textField1.getText().length())<10){//姓名有数据，即修改
                            imf[j]=imf[j]+textField1.getText();
                            for (int i = 0; i < len; i++)
                                imf[j]=imf[j]+" ";
                        }
                        else
                            imf[j]=imf[j]+s.substring(0, 10);//没有就加原来的
                       // System.out.println(s.substring(0, 9));
                        imf[j]=imf[j]+s.substring(10, 20);//学号加原来的
                        if((len = 10 - textField3.getText().length())<10){//语文有数据，即修改
                            imf[j]=imf[j]+textField3.getText();
                            for (int i = 0; i < len; i++)
                                imf[j]=imf[j]+" ";
                        }
                        else
                            imf[j]=imf[j]+s.substring(20, 30);//没有就加原来的
                        if((len = 10 - textField4.getText().length())<10){//数学有数据，即修改
                            imf[j]=imf[j]+textField4.getText();
                            for (int i = 0; i < len; i++)
                                imf[j]=imf[j]+" ";
                        }
                        else
                            imf[j]=imf[j]+s.substring(30,40);//没有就加原来的
                        if((len = 10 - textField5.getText().length())<10){//英语有数据，即修改
                            imf[j]=imf[j]+textField5.getText();
                            for (int i = 0; i < len; i++)
                                imf[j]=imf[j]+" ";
                        }
                        else
                            imf[j]=imf[j]+s.substring(40, 50);//没有就加原来的
                        if((len = 10 - textField6.getText().length())<10){//体育有数据，即修改
                            imf[j]=imf[j]+textField6.getText();
                            for (int i = 0; i < len; i++)
                                imf[j]=imf[j]+" ";
                        }
                        else
                            imf[j]=imf[j]+s.substring(50, 60);//没有就加原来的
                        j++;
                        exsist=true;//修改成功
                    }
                    else
                        imf[j++] = "" + s;
                }
                fbr.close();
                if (exsist) {
                    BufferedWriter inbf = new BufferedWriter(new FileWriter("src/file/Grade.txt"));
                    for (int i = 0; i < j; i++){
                        inbf.write(imf[i]);
                        inbf.newLine();
                    }
                    inbf.close();
                    JOptionPane.showMessageDialog(null, "修改成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
                } else
                    JOptionPane.showMessageDialog(null, "不存在该生信息！", "提示", JOptionPane.WARNING_MESSAGE);
            } catch (Exception ee) {
                ee.printStackTrace();
            }
        }
    }

    private void button1(ActionEvent e) {
        dispose();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        panel1 = new JPanel();
        textField1 = new JTextField();
        label1 = new JLabel();
        textField2 = new JTextField();
        label2 = new JLabel();
        textField3 = new JTextField();
        label3 = new JLabel();
        textField4 = new JTextField();
        label4 = new JLabel();
        textField5 = new JTextField();
        label5 = new JLabel();
        label6 = new JLabel();
        ok = new JButton();
        button1 = new JButton();
        textField6 = new JTextField();
        bgp = new JLabel();

        //======== this ========
        setTitle("\u4fee\u6539\u5b66\u751f\u4fe1\u606f");
        setVisible(true);
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== panel1 ========
        {
            panel1.setLayout(null);
            panel1.add(textField1);
            textField1.setBounds(160, 35, 115, 30);

            //---- label1 ----
            label1.setText("\u59d3\u540d\uff1a");
            label1.setForeground(Color.black);
            panel1.add(label1);
            label1.setBounds(115, 40, 40, 25);
            panel1.add(textField2);
            textField2.setBounds(160, 75, 115, 30);

            //---- label2 ----
            label2.setText("\u5b66\u53f7\uff1a");
            label2.setForeground(Color.black);
            panel1.add(label2);
            label2.setBounds(115, 80, 40, 25);
            panel1.add(textField3);
            textField3.setBounds(160, 110, 115, 30);

            //---- label3 ----
            label3.setText("\u8bed\u6587\uff1a");
            label3.setForeground(Color.black);
            panel1.add(label3);
            label3.setBounds(115, 115, 40, 25);
            panel1.add(textField4);
            textField4.setBounds(160, 145, 115, 30);

            //---- label4 ----
            label4.setText("\u6570\u5b66\uff1a");
            label4.setForeground(Color.black);
            panel1.add(label4);
            label4.setBounds(115, 150, 40, 25);
            panel1.add(textField5);
            textField5.setBounds(160, 180, 115, 30);

            //---- label5 ----
            label5.setText("\u82f1\u8bed\uff1a");
            label5.setForeground(Color.black);
            panel1.add(label5);
            label5.setBounds(115, 185, 40, 25);

            //---- label6 ----
            label6.setText("\u4f53\u80b2\uff1a");
            label6.setForeground(Color.black);
            panel1.add(label6);
            label6.setBounds(115, 220, 40, 25);

            //---- ok ----
            ok.setText("\u4fee\u6539");
            ok.setBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED));
            ok.setContentAreaFilled(false);
            ok.addActionListener(e -> {
			ok(e);
		});
            panel1.add(ok);
            ok.setBounds(115, 270, 70, 25);

            //---- button1 ----
            button1.setText("\u8fd4\u56de");
            button1.setContentAreaFilled(false);
            button1.setBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED));
            button1.addActionListener(e -> button1(e));
            panel1.add(button1);
            button1.setBounds(215, 270, 70, 25);
            panel1.add(textField6);
            textField6.setBounds(160, 220, 115, 30);

            //---- bgp ----
            bgp.setHorizontalAlignment(SwingConstants.CENTER);
            bgp.setIcon(new ImageIcon(getClass().getResource("/myimage/loginimg.jpg")));
            panel1.add(bgp);
            bgp.setBounds(0, -5, 390, 330);
        }
        contentPane.add(panel1);
        panel1.setBounds(0, 0, 390, 325);

        contentPane.setPreferredSize(new Dimension(390, 355));
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel panel1;
    private JTextField textField1;
    private JLabel label1;
    private JTextField textField2;
    private JLabel label2;
    private JTextField textField3;
    private JLabel label3;
    private JTextField textField4;
    private JLabel label4;
    private JTextField textField5;
    private JLabel label5;
    private JLabel label6;
    private JButton ok;
    private JButton button1;
    private JTextField textField6;
    private JLabel bgp;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
